<?php

function holdersize($hold , $holdersize = 0) { 
    if(@$handle = opendir($hold)) {
        while(false !== ($file = readdir($handle))) {
            if ($file != "." && $file != "..") {
                if(is_dir( $hold.'/'.$file)) {
                    $holdersize = holdersize($hold.'/'.$file , $holdersize);
                }else {
                    $holdersize = $holdersize + filesize($hold.'/'.$file);
                }
            }
        }
    }else {
        $holdersize = '无法获取'.$hold.'目录';
    }

    return $holdersize;
}

function get_used_space() {
    $size = holdersize(ROOT);
    return $size;
}

function get_space_info() {
    $orginal_space_size = get_used_space();
    $space_size = al_get_size(get_option('space_size'), 'B');
    $info->total = al_get_size($space_size);
    $info->percent = round(($orginal_space_size / $space_size), 4) * 100;
    $info->used = al_get_size($orginal_space_size);
    return $info;
}

function get_database_info() {
    global $aldb;
    $orginal_database_size = get_used_database();
    $database_size = al_get_size(get_option('database_size'), 'B');
    $info->total = al_get_size($database_size);
    $info->percent = round(($orginal_database_size / $database_size ), 4) * 100;
    $info->used = al_get_size($orginal_database_size);
    return $info;
}

function get_used_database() {
    global $aldb;
    $datasize = 0;
    $indexsize = 0;
    $tables = $aldb->get_results("SHOW TABLE STATUS");
    foreach($tables as $table) {
        $datasize  += $table->Data_length;
        $indexsize += $table->Index_length;
    }

    return $datasize + $indexsize;
}

function get_notice_color($number) {
    $color = '#0a0';
    if ($number < 50) {
        $color = '#0a0';
    } else if ($number >= 85) {
        $color = '#ff0000';
    } else {
        $color = '#ff9900';
    }
    return $color;
}
?>
